package gui.util;

import gui.browse.Browse;
import gui.browse.BrowseListener;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import controller.AdministradorController;
import controller.ControllerBrowse;

public abstract class JFrameCadastro extends JFrame {
	private static final long serialVersionUID = 1L;

	public JPanel panelRodape;
	public JButton botaoIncluir;
	public JButton botaoAlterar;
	public JButton botaoExcluir;
	public JButton botaoCancelar;
	public JButton botaoFechar;
	public JButton botaoGravar;
	public JButton botaoBrowse;

	public abstract void preencherCampos(HashMap<String, Object> dados);

	public abstract ActionListener getActionIncluir();

	public abstract ActionListener getActionAlterar();

	public abstract ActionListener getActionExcluir();

	public abstract ActionListener getActionCancelar();

	public abstract ActionListener getActionGravar();

	public abstract JPanel getPanelCentral();

	public JFrameCadastro() {
		initComponents();
	}

	public void initComponents() {
		this.setLayout(new BorderLayout());
		this.add(getPanelCentral(), BorderLayout.CENTER);
		this.add(getPanelRodape(), BorderLayout.SOUTH);

		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setResizable(false);
		pack();
		centralizar();
		this.setVisible(true);
	}

	public void centralizar() {
		Dimension so = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(((so.width - this.getSize().width) / 2),
				((so.height - this.getSize().height) / 2));
	}

	public JPanel getPanelRodape() {
		if (panelRodape == null) {
			panelRodape = new JPanel();

			panelRodape.add(getBotaoIncluir());
			panelRodape.add(getBotaoAlterar());
			panelRodape.add(getBotaoExcluir());
			panelRodape.add(getBotaoCancelar());
			panelRodape.add(getBotaoGravar());
			panelRodape.add(getBotaoFechar());
		}
		return panelRodape;
	}

	public JButton getBotaoIncluir() {
		if (botaoIncluir == null) {
			botaoIncluir = new JButton("Incluir", new ImageIcon(
					"icones/adicionar.png"));
			botaoIncluir.addActionListener(getActionIncluir());
		}
		return botaoIncluir;
	}

	public JButton getBotaoAlterar() {
		if (botaoAlterar == null) {
			botaoAlterar = new JButton("Alterar", new ImageIcon(
					"icones/alterar.png"));
			botaoAlterar.addActionListener(getActionAlterar());
		}
		return botaoAlterar;
	}

	public JButton getBotaoExcluir() {
		if (botaoExcluir == null) {
			botaoExcluir = new JButton("Excluir", new ImageIcon(
					"icones/excluir.png"));
			botaoExcluir.addActionListener(getActionExcluir());
		}
		return botaoExcluir;
	}

	public JButton getBotaoCancelar() {
		if (botaoCancelar == null) {
			botaoCancelar = new JButton("Cancelar", new ImageIcon(
					"icones/cancelar.png"));
			botaoCancelar.setEnabled(false);
			botaoCancelar.addActionListener(getActionCancelar());
		}
		return botaoCancelar;
	}

	public JButton getBotaoGravar() {
		if (botaoGravar == null) {
			botaoGravar = new JButton("Gravar", new ImageIcon(
					"icones/gravar.png"));
			botaoGravar.setEnabled(false);
			botaoGravar.addActionListener(getActionGravar());
		}
		return botaoGravar;
	}

	public JButton getBotaoFechar() {
		if (botaoFechar == null) {
			botaoFechar = new JButton("Fechar", new ImageIcon(
					"icones/fechar.png"));
			botaoFechar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					dispose();
				}
			});
		}
		return botaoFechar;
	}
	
	ControllerBrowse controller = null;
	public JButton getBotaoBrowse(ControllerBrowse control) {
		this.controller = control;
		
		if (botaoBrowse == null) {
			botaoBrowse = new JButton("...");
			botaoBrowse.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {				
					Browse browse = new Browse(controller);
					browse.addBrowseListener(new BrowseListener() {

						@Override
						public void linhaClicada(HashMap<String, Object> dados) {
							preencherCampos(dados);

						}
					});
					browse.getFocusableWindowState();
				}
			});
		}
		return botaoBrowse;
	}
}
